package cn.xk.org.controller;

import cn.xk.basic.util.AjaxResult;
import cn.xk.basic.util.ExcelUtils;
import cn.xk.basic.util.PageList;
import cn.xk.org.domain.Shop;
import cn.xk.org.domain.ShopAuditLog;
import cn.xk.org.query.ShopQuery;
import cn.xk.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 后端接口类；
 */
@RestController
@RequestMapping("/shop")
public class ShopController {

    @Autowired
    private IShopService shopService;


    /**
     * 接口：添加或修改
     *
     * @param shop 传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop) {
        try {
            if (shop.getId() != null)
                shopService.update(shop);
            else
                shopService.save(shop);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "操作失败");
        }
    }

    /**
     * 接口：删除
     *
     * @param id
     * @return AjaxResult 响应给前端
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            shopService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "删除失败");
        }
    }

    /**
     * 接口：批量删除
     *
     * @param ids
     * @return AjaxResult 响应给前端
     */
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids) {
        try {
            shopService.patchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "批量删除失败");
        }
    }

    /**
     * 接口：查询单个对象
     *
     * @param id
     */
    @GetMapping("/{id}")
    public Shop findOne(@PathVariable("id") Long id) {
        return shopService.loadById(id);
    }


    /**
     * 接口：查询所有
     *
     * @return
     */
    @GetMapping
    public List<Shop> findAll() {
        return shopService.loadAll();
    }


    /**
     * 接口：分页查询或高级查询
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping
    public PageList<Shop> queryPage(@RequestBody ShopQuery query) {
        return shopService.queryPage(query);
    }

    /**
     * 店铺入驻业务
     *
     * @param shop
     * @return
     */
    @PostMapping(value = "/settlement")
    public AjaxResult settlement(@RequestBody Shop shop) {
        try {
            shopService.settlement(shop);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        }
    }

    @PostMapping(value = "/audit/pass")
    public AjaxResult auditPass(@RequestBody ShopAuditLog log) {
        try {
            shopService.auditPass(log);
            return new AjaxResult();

        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        }
    }

    @PostMapping(value = "/audit/reject")
    public AjaxResult auditReject(@RequestBody ShopAuditLog log) {
        try {
            shopService.auditReject(log);
            return new AjaxResult();

        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        }
    }

    @GetMapping("/export")
    public void export(HttpServletResponse response) {
        List<Shop> shops = shopService.loadAll();
        //调用工具类导出excel
        //(List<?> list, String title, String sheetName, Class<?> pojoClass,String fileName,HttpServletResponse response)
        //导出的数据          标题          sheetName页名称    导出的类型          文件名                响应对象
        //文件名如果不写后缀默认是xls，可以写上xlsx就是指定的xlsx类型
        ExcelUtils.exportExcel(shops, null, "店铺信息", Shop.class, "shop.xlsx", response);
    }

    @PostMapping("/importExcel")
    public void importExcel(@RequestPart("file") MultipartFile file) {
        //String file = "F:\\文件名.xls";
        //解析excel，MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass
        //             文件对象             忽略标题行数        表头行数             生成的对象类型
        List<Shop> list = ExcelUtils.importExcel(file, 0, 1, Shop.class);
        System.out.println("导入数据一共【" + list.size() + "】行");
        for (Shop shop : list) {
            System.out.println(shop);
        }

    }

     @GetMapping("/active/{id}")
     public String activeId(@PathVariable("id") Long id) {
         return shopService.activeId(id);
     }

}


